package com.fanbai.score.user.fragemnt;

/**
 * Created by acer on 2017/5/6.
 */

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.fanbai.score.R;
import com.fanbai.score.common.Utils.ImageUtils;
import com.fanbai.score.common.Utils.ToastUtils;
import com.fanbai.score.common.activity.CommonList_Activity;
import com.fanbai.score.common.activity.WebView_Activity;
import com.fanbai.score.common.base.BaseFragment;
import com.fanbai.score.common.http.CallBack;
import com.fanbai.score.common.view.NoScrollListView;
import com.fanbai.score.user.bean.ActInfo_Bean;
import com.fanbai.score.user.bean.ShopClass_Bean;
import com.fanbai.score.user.http.UserActivity_RequestNet_Utils;
import com.fanbai.score.user.http.UserRequestNetwork_Utils;
import com.fanbai.score.user.http.UserScorea_RequestNet_Utils;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * A placeholder fragment containing a simple view.
 */
public class Act_Fragment extends BaseFragment {

    @BindView(R.id.view_recyclerView)
    RecyclerView mRecyclerView;

    @BindView(R.id.userFragmentAct_duiHuanYQM)
    TextView dhyqm;
    @BindView(R.id.userFragmentAct_LQJF)
    TextView lqjf;
    @BindView(R.id.userFragmentAct_LQGRB)
    TextView lqgrb;

    @OnClick({R.id.userFragmentAct_duiHuanYQM, R.id.userFragmentAct_LQJF, R.id.userFragmentAct_LQGRB})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.userFragmentAct_duiHuanYQM:
                Bundle bundle = new Bundle();
                bundle.putString("url", "http://score.xinyou777.cn/customer/activity/duihuanactivity.php");
                ctx.jumpToActivity(WebView_Activity.class, bundle);
                break;
            case R.id.userFragmentAct_LQJF:
                pickupEveryScore();
                break;
            case R.id.userFragmentAct_LQGRB:
                pickupEveryCoin();
                break;
        }
    }

    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static Act_Fragment newInstance(int sectionNumber) {
        Act_Fragment fragment = new Act_Fragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public Act_Fragment() {
    }


    @Override
    protected int getLayoutID() {
        return R.layout.user_fragment_act;
    }

    @Override
    public String getTitleName() {
        return "活动";
    }

    @Override
    public void iniTitleLeft(ImageView imageView) {
    }

    @Override
    public void iniTitleRight(TextView tv) {

    }

    @Override
    protected void initView(View view) {
    }

    @Override
    protected void initData() {
        requestActList();
        inviteCodeStart();
    }

    @Override
    protected void initListener() {

    }

    private void setData(final List<ShopClass_Bean> beans){
//        List<String> dataList = new ArrayList<>();
//        dataList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1494156320411&di=ee09c92ad8fa24ca4fe0efb54d902133&imgtype=0&src=http%3A%2F%2Fimg.newmotor.com.cn%2FUploadFiles%2Fimage%2F20160506%2F20160506114832563256.png");
//        dataList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b10000_10000&sec=1494146013&di=8cdae46fd79675fb1e22e24572d7f578&src=http://www.moto2s.com/ziliao/59/2012102014411842848.jpg");
//        dataList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b10000_10000&sec=1494146013&di=8cdae46fd79675fb1e22e24572d7f578&src=http://www.moto2s.com/ziliao/59/2012102014411842848.jpg");
        mRecyclerView.setLayoutManager(new LinearLayoutManager(ctx));
        CommonAdapter adapter = new CommonAdapter<ShopClass_Bean>(ctx, R.layout.item_imgview,
                beans){
            @Override
            protected void convert(ViewHolder holder, ShopClass_Bean bean, int position) {
                ImageUtils.show(ctx, bean.getClslogoimage(), (ImageView)holder.getView(R.id.itemImgView),  R.mipmap.ic_launcher);
//                holder.setText(R.id.itemTextView, bean.getClsname());
            }
        };
        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                requestActItemList(beans.get(position).getClsid());
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        mRecyclerView.setAdapter(adapter);
    }

    /**
     * 分类列表
     */
    private void requestActList(){
        UserActivity_RequestNet_Utils.getActClassInfos(ctx, new CallBack<List<ShopClass_Bean>>() {
            @Override
            public void onSuccess(String code, String msg, List<ShopClass_Bean> beans) {
                setData(beans);
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                ToastUtils.WarnImageToast(ctx, msg);
            }

            @Override
            public void onError(String code, String msg) {
                ToastUtils.ErrorImageToast(ctx, msg);
            }
        });
    }

    private void requestActItemList(final String clsid){
        ctx.showDialog();
        UserActivity_RequestNet_Utils.getActivityInfosByClsid(ctx, clsid, new CallBack<ArrayList<ActInfo_Bean>>() {
            @Override
            public void onSuccess(String code, String msg, ArrayList<ActInfo_Bean> beans) {
                ctx.hideDialog();
                if(beans != null && beans.size() == 1){
                    Bundle bundle = new Bundle();
                    bundle.putString("url", beans.get(0).getActurl());
                    ctx.jumpToActivity(WebView_Activity.class, bundle);
                }else{
                    Bundle bundle = new Bundle();
                    bundle.putString("url", "http://score.xinyou777.cn/customer/activity/activitylist.php?clsid="+clsid);
                    ctx.jumpToActivity(WebView_Activity.class, bundle);
                }
                /*Bundle bundle = new Bundle();
                bundle.putString("status", "actList");
                bundle.putParcelableArrayList("beans", beans);
                ctx.jumpToActivity(CommonList_Activity.class, bundle);*/
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                ctx.hideDialog();
                ToastUtils.WarnImageToast(ctx, msg);
            }

            @Override
            public void onError(String code, String msg) {
                ctx.hideDialog();
                ToastUtils.ErrorImageToast(ctx, msg);
            }
        });
    }

    private void inviteCodeStart(){
        UserRequestNetwork_Utils.inviteCodeStart(ctx, new CallBack<String>() {
            @Override
            public void onSuccess(String code, String msg, String s) {
                if(s.equals("1")){
                    dhyqm.setVisibility(View.VISIBLE);
                }else{
                    dhyqm.setVisibility(View.GONE);
                }
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                ToastUtils.WarnImageToast(ctx, msg);
            }

            @Override
            public void onError(String code, String msg) {
                ToastUtils.ErrorImageToast(ctx, msg);
            }
        });
    }

    private void pickupEveryCoin(){
        ctx.showDialog();
        UserScorea_RequestNet_Utils.pickupEveryCoin(ctx, new CallBack<String>() {
            @Override
            public void onSuccess(String code, String msg, String s) {
                ctx.hideDialog();
                ToastUtils.RightImageToast(ctx, msg);
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                ctx.hideDialog();
                ToastUtils.WarnImageToast(ctx, msg);
            }

            @Override
            public void onError(String code, String msg) {
                ctx.hideDialog();
                ToastUtils.ErrorImageToast(ctx, msg);
            }
        });
    }
    private void pickupEveryScore(){
        ctx.showDialog();
        UserScorea_RequestNet_Utils.pickupEveryScore(ctx, new CallBack<String>() {
            @Override
            public void onSuccess(String code, String msg, String s) {
                ctx.hideDialog();
                ToastUtils.RightImageToast(ctx, msg);
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                ctx.hideDialog();
                ToastUtils.WarnImageToast(ctx, msg);
            }

            @Override
            public void onError(String code, String msg) {
                ctx.hideDialog();
                ToastUtils.ErrorImageToast(ctx, msg);
            }
        });
    }
}
